package com.hook.analyse.util;
import android.content.pm.*;
import com.hook.analyse.model.*;
import java.util.*;
import android.util.*;
import java.text.*;

public class Apptool {
    static  String TAG = "ApkTool";
    public static List<AppInfo> mLocalInstallApps = null;

    public static List<AppInfo> scanLocalInstallAppList(PackageManager packageManager) {
        List<AppInfo> myAppInfos = new ArrayList<AppInfo>();
        try {
            List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0);
            for (int i = 0; i < packageInfos.size(); i++) {
                PackageInfo packageInfo = packageInfos.get(i);
                //过滤掉系统app
            if ((ApplicationInfo.FLAG_SYSTEM & packageInfo.applicationInfo.flags) != 0) {
                continue;
            }
                AppInfo myAppInfo = new AppInfo();
                myAppInfo.packageName=(packageInfo.packageName);
                if (packageInfo.applicationInfo.loadIcon(packageManager) == null) {
                    continue;
                }
				myAppInfo.appName=packageManager.getApplicationLabel(packageInfo.applicationInfo).toString();
                myAppInfo.img=(packageInfo.applicationInfo.loadIcon(packageManager));
                myAppInfos.add(myAppInfo);
            }
        }catch (Exception e){
            Log.e(TAG,"===============获取应用包信息失败");
        }
		
//按照名称排序
		Collections.sort(myAppInfos, new Comparator<AppInfo>() {
				@Override
				public int compare(AppInfo o1, AppInfo o2) {
					//排序规则：按照汉字拼音首字母排序
					Comparator<Object> com = Collator.getInstance(java.util.Locale.CHINA);
					return com.compare(o1.appName, o2.appName);

				}
			});
		
		
        return myAppInfos;
    }
 
}
